<template>
  <view class="container">
    <view class="content">
      <view class="section-title">
        <view class="blue-bar"></view>
        <text>线下支付配置</text>
      </view>

      <view class="form-section">
        <u-form ref="formdata" :model="form" :rules="rules" labelPosition="left" labelWidth="150">
          <u-form-item label="三方支付子商户号" prop="subPayNo">
            <u-input v-model="form.subPayNo" placeholder="请输入" border="bottom" clearable />
          </u-form-item>
          <u-form-item label="多少分1块钱" prop="integralPropertyConversion">
            <u-input v-model="form.integralPropertyConversion" placeholder="请输入" type="number" border="bottom" clearable @blur="formatNumber('integralPropertyConversion')" />
          </u-form-item>
          <u-form-item label="抵扣类型设置" prop="offlineDeductionPropertyType">
            <!--  0：不使用 1：固定值 2：比例 -->
            <u-radio-group size="30" v-model="form.offlineDeductionPropertyType" placement="row">
              <u-radio size="30" iconSize="20" labelSize="30" :name="1" label="固定值"></u-radio>
              <u-radio size="30" iconSize="20" labelSize="30" :name="2" label="比例"></u-radio>
              <u-radio size="30" iconSize="20" labelSize="30" :name="0" label="不使用"></u-radio>
            </u-radio-group>
          </u-form-item>
          <u-form-item v-if="form.offlineDeductionPropertyType == 1" label="最大抵扣金额" prop="offlineIntegralDeductionPropertyValue">
            <u-input
              v-model="form.offlineIntegralDeductionPropertyValue"
              placeholder="请输入"
              type="digit"
              border="bottom"
              clearable
              @blur="formatNumber('offlineIntegralDeductionPropertyValue')"
            />
          </u-form-item>
          <u-form-item v-if="form.offlineDeductionPropertyType == 2" label="抵扣比例" prop="offlineIntegralProperty">
            <u-input v-model="form.offlineIntegralProperty" placeholder="请输入" type="digit" border="bottom" clearable @blur="formatNumber('offlineIntegralProperty')" />
          </u-form-item>
          <u-form-item label="平台抽成比例" prop="lsSplittingBl">
            <u-input v-model="form.lsSplittingBl" placeholder="请输入" border="bottom" clearable />
          </u-form-item>
        </u-form>
      </view>

      <!--  <view class="section-title">
        <view class="blue-bar"></view>
        <text>消费会员配置</text>
      </view>
 -->

      <view class="button-group">
        <u-button @click="goBack" plain>返 回</u-button>
        <u-button type="primary" @click="handleConfirm">保 存</u-button>
      </view>
    </view>
  </view>
</template>

<script>
import { updateMer1, setWxPayConfig } from '../../utils/merorder/mer';
import { getUserInfo } from '../../utils/merorder/mer';
export default {
  name: 'OfflineStore',
  data() {
    return {
      userType: 0,
      rowData: null,
      form: {},
      rules: {
        /*   offlineIntegralProportionBack: [
          { required: true, message: '请输入线下支付返还比例', trigger: ['blur', 'change'] },
          { pattern: /^[0-9]+(.[0-9]{1,4})?$/, message: '请输入正确的数字', trigger: ['blur', 'change'] }
        ],
        offlineIntegralProperty: [
          { required: true, message: '请输入线下折扣比例', trigger: ['blur', 'change'] },
          { pattern: /^[0-9]+(.[0-9]{1,4})?$/, message: '请输入正确的数字', trigger: ['blur', 'change'] }
        ],
        lsSplittingBlOrdinary: [
          { required: true, message: '请输入消费订单抽成', trigger: ['blur', 'change'] },
          { pattern: /^[0-9]+(.[0-9]{1,4})?$/, message: '请输入正确的数字', trigger: ['blur', 'change'] }
        ],
        lsSplittingBl: [
          { required: true, message: '请输入物业订单抽成', trigger: ['blur', 'change'] },
          { pattern: /^[0-9]+(.[0-9]{1,4})?$/, message: '请输入正确的数字', trigger: ['blur', 'change'] }
        ],
        subPayNo: [
          { required: true, message: '请输入三方支付子商户号', trigger: ['blur', 'change'] },
          { pattern: /^[0-9]+(.[0-9]{1,4})?$/, message: '请输入正确的数字', trigger: ['blur', 'change'] }
        ],
        isOfflineIntegralProportionBack: [{ required: true, message: '请选择是否返回积分', trigger: ['blur', 'change'] }] */
      }
    };
  },
  onLoad(options) {
    // 获取传递的数据
    if (options.rowData) {
      this.rowData = JSON.parse(options.rowData);
      this.form.merId = this.rowData.id;
      // 深拷贝避免修改原数据
      this.form = Object.assign({}, this.form, this.rowData);
    }
    getUserInfo(uni.getStorageSync('merId')).then((res) => {
      this.form = res.data;
      // 模拟余额数据（实际应从接口获取）
    });
    // 获取用户类型
    const userInfo = uni.getStorageSync('userInfo');
    if (userInfo && userInfo.userType) {
      this.userType = userInfo.userType;
    }
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },
    handleConfirm() {
      /* this.$refs['formdata']
        .validate()
        .then(() => { */
      updateMer1(this.form).then(() => {
        if (this.form.subPayNo) {
          setWxPayConfig(this.form.subPayNo).then((res) => {
            if (res.code == 200) {
              this.$u.toast('保存成功');
              uni.setStorageSync('integralPropertyConversion', this.form.integralPropertyConversion);
              uni.setStorageSync('offlineDeductionPropertyType', this.form.offlineDeductionPropertyType);
              // 返回上一页并刷新数据
              setTimeout(() => {
                uni.navigateBack();
              }, 1000);
            }
          });
        } else {
          this.$u.toast('保存成功');
          // 返回上一页并刷新数据
          setTimeout(() => {
            uni.navigateBack();
          }, 1000);
        }
      });
      /* })
        .catch((errors) => {
          this.$u.toast('请填写完整信息');
        }); */
    },
    formatNumber(field) {
      const value = parseFloat(this.form[field]);
      if (!isNaN(value)) {
        this.form[field] = Number(value) /* .toFixed(4) */;
      } else {
        this.form[field] = '';
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  flex: 1;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.page-header {
  background-color: #fff;
  padding: 30rpx;
  border-bottom: 1rpx solid #eee;
}

.page-title {
  font-size: 36rpx;
  font-weight: bold;
  text-align: center;
}

.content {
  padding: 30rpx;
}

.section-title {
  display: flex;
  align-items: center;
  margin: 30rpx 0 20rpx;
}

.blue-bar {
  width: 8rpx;
  height: 32rpx;
  background-color: #409eff;
  margin-right: 16rpx;
}

.form-section {
  background-color: #fff;
  border-radius: 10rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.button-group {
  display: flex;
  justify-content: space-between;
  margin-top: 60rpx;

  ::v-deep .u-button {
    flex: 1;
    margin: 0 20rpx;
    height: 80rpx;
    border-radius: 40rpx;
  }
}
</style>
